//指定中文输出编码
phantom.outputEncoding="GBK";

var limit_num = [];

var fs = require('fs');

var casper = require('casper').create();

casper.start('http://www.bjjtgl.gov.cn/');

var limit_text = "";
var separator = '和'

function formatDate(date){
	return date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
}

casper.then(function(){
	if(this.exists("span#todaynum.todaynum")){
		limit_text = this.evaluate(function(){
			return $('span#todaynum.todaynum').html();
		});
	}
	
	this.echo(formatDate(new Date()) + " 限行号码是" + limit_text);
	if(limit_text.length===0){
		this.echo("没有抓到数据");
	}else{
		limit_num = limit_text.split(separator);
		var data = JSON.stringify({
			date:formatDate(new Date()),
			limitNo:limit_num
		});
		//将限号数据存入文件
		fs.write('limit_num.txt',data + "\r\n",'a');
	}
});

casper.run();
